package com.tensai.study.java.opensource.hutool;

import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

/**
 * 字节转换
 *
 * @author Tensai
 */
public class BytesToFloatTest {

	@Test
	public void test() throws IOException {
		float s = getFloat(new byte[]{0x43, 0x5e, (byte) 0xb3, 0x33});
		System.out.println(s);
		DataInputStream dis = new DataInputStream(new ByteArrayInputStream(new byte[]{0x43, 0x5e, (byte) 0xb3, 0x33}));
		float f = dis.readFloat();
		System.out.println(f);
	}

	public float getFloat(byte[] b) {
		// 4 bytes
		int accum = 0;
		for (int shiftBy = 0; shiftBy < b.length; shiftBy++) {
			accum |= (b[shiftBy] & 0xff) << shiftBy * 8;
		}
		return Float.intBitsToFloat(accum);
	}
}
